
import java.util.concurrent.PriorityBlockingQueue;

class MyTask implements Comparable<MyTask> {
    Runnable task;
    long time;
    public MyTask(Runnable task,long data) {
        this.task = task;
        this.time = System.currentTimeMillis() + data;
    }
    @Override
    public int compareTo(MyTask o) {
        return (int)(this.time - o.time);
    }
}

class MyTimer {
    Object key = new Object();
    PriorityBlockingQueue<MyTask> queue = new PriorityBlockingQueue<>();
    public void schedule(Runnable task,long data) {
        MyTask myTask = new MyTask(task,data);
        queue.put(myTask);
        synchronized(key) {
            key.notify();
        }
    }
    public MyTimer() {
        Thread t = new Thread(() -> {
            while(true) {
                synchronized (key) {
                    try {
                        MyTask myTask = queue.take();
                        long curTime = System.currentTimeMillis();
                        if (curTime - myTask.time > 0) {
                            myTask.task.run();
                        } else {
                            queue.put(myTask);
                            key.wait(myTask.time - curTime);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        t.start();
    }
}

public class Text {
    public static void main(String[] args) {
        MyTimer myTimer = new MyTimer();
        myTimer.schedule(new Runnable(){
            @Override
            public void run() {
                System.out.println("heieh");
            }
        },3000);
        myTimer.schedule(new Runnable(){
            @Override
            public void run() {
                System.out.println("haha");
            }
        },2000);
        myTimer.schedule(new Runnable(){
            @Override
            public void run() {
                System.out.println("hello");
            }
        },1000);

    }
}
